package main

import (
	pb "PredictionProto/service/gen/PredictionProto"

	"PredictionProto/service/client/basic"
	"context"
	"encoding/json"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"time"
)

const (
	address = "172.20.51.6:31616"
)

func main() {
	auth := basic.Auth{
		RequestId: "3ec61d89-4a05-49a6-92d1-da22db690c4b",
		ApiUri:    "/aps/msserver/service/078f31aa-1dd6-4ae7-92e0-f300d381eef1",
		ApiKey:    "7bccfaf5-6552-4cfe-813f-f60fdb51728c",
	}

	// Set up a connection to the product-server.
	conn, err := grpc.Dial(address, grpc.WithPerRPCCredentials(auth), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer func(conn *grpc.ClientConn) {
		log.Println("close connection")
		err := conn.Close()
		if err != nil {
			log.Fatalf("Failed to close connection: %v", err)
			return
		}
	}(conn)
	c := pb.NewPredictionClient(conn)
	// Contact the product-server and print out its response.

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*500)
	defer cancel()
	p := &pb.PredictionMessage{
		DataOneof: &pb.PredictionMessage_Dataframe{
			Dataframe: &pb.Dataframe{
				Names: []string{"text"},
				Values: &pb.ListValue{
					Values: []*pb.Value{
						{
							Kind: &pb.Value_StringValue{
								StringValue: "银",
							},
						},
					},
				},
			},
		},
	}
	product, err := c.Predict(ctx, p)
	if err != nil {
		log.Fatalf("Could not get product: %v", err)
	}
	marshal, _ := json.MarshalIndent(product, "", " ")
	log.Printf("Product: %s \r\n", string(marshal))

}
